﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameJamKingdom
{
    class GameOverState : State
    {
        SpriteFont font;
        KeyboardState cKeyState;
        Texture2D hiscoreImage;
        Vector2 scorePos;
        int score;
        int hiscore;
        Vector2 hiscorePos;
        string scoreFilePath;
        int timeElapsed;

        Texture2D snake;
        Rectangle snakeRect;

        public GameOverState(Game1 cGame)
        {
            Initialize(cGame);
        }

        public override void Initialize(Game1 cGame)
        {
            font = cGame.Content.Load<SpriteFont>("ScoreFont");
            hiscoreImage = cGame.Content.Load<Texture2D>("HighScore");
            scorePos = new Vector2(418, 330);
            hiscore = getHiscore();

            snake = cGame.Content.Load<Texture2D>("SnakeBite");
            snakeRect = new Rectangle(800, 0, 400, 300);
        }

        public override void Update(Game1 cGame, GameTime gameTime)
        {
            score = cGame.finalScore;
            
            cKeyState = Keyboard.GetState();
            timeElapsed += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            if ((cKeyState.IsKeyDown(Keys.Enter) || (cKeyState.IsKeyDown(Keys.Left)) || (cKeyState.IsKeyDown(Keys.Escape))) && (timeElapsed > 100))
            {
                timeElapsed = 0;
                if (score > hiscore)
                {
                    hiscore = score;
                    setHiscore();
                }

                cGame.cState = "menu";
                cGame.stateDict["gameplay"].Initialize(cGame);
                cGame.stateDict[cGame.cState].Initialize(cGame);
            }

            float snakeDip = 600.0f * (float)Math.Sin((gameTime.TotalGameTime.TotalSeconds % 5) * 3.14);
            snakeRect.X = 1000 - (int)snakeDip;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            hiscorePos = new Vector2(Program.wWidth / 2 - font.MeasureString(hiscore.ToString()).X / 2, Program.wHeight / 2 - 30);
            spriteBatch.Begin();
            
            spriteBatch.Draw(hiscoreImage, new Vector2(0, 0), Color.White);
            spriteBatch.DrawString(font, hiscore.ToString(), hiscorePos, Color.Red);
            spriteBatch.DrawString(font, score.ToString(), scorePos, Color.Black);
            spriteBatch.Draw(snake, snakeRect, Color.White);
            spriteBatch.End();
        }

        private int getHiscore()
        {
            scoreFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\SnakeRunnerHiscore.txt";
            if (File.Exists(scoreFilePath))
            {
                using (StreamReader sr = new StreamReader(scoreFilePath))
                {
                    String line;
                    line = sr.ReadLine();
                    return Convert.ToInt32(line);
                }
            }
            else
            {
                return 0;
            }
        }

        private void setHiscore()
        {
            using (StreamWriter sw = new StreamWriter(scoreFilePath))
            {
                sw.Write(hiscore.ToString());
            }
        }
    }
}
